package cn.tedu.psychology.service.impl;

import cn.tedu.psychology.ex.ServiceException;
import cn.tedu.psychology.mapper.QuestionMapper;
import cn.tedu.psychology.pojo.dto.QuestionAddNewDTO;
import cn.tedu.psychology.pojo.entity.Question;
import cn.tedu.psychology.pojo.vo.QuestionListItemVO;
import cn.tedu.psychology.pojo.vo.QuestionStandardVO;
import cn.tedu.psychology.service.IQuestionService;
import cn.tedu.psychology.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public void addNew(QuestionAddNewDTO questionAddNewDTO) {
        Question question = new Question();
        BeanUtils.copyProperties(questionAddNewDTO, question);
        question.setCreateTime(LocalDateTime.now());
        System.out.println(question);
        questionMapper.insert(question);
    }

    @Override
    public QuestionStandardVO getStandardById(Long id) {
        QuestionStandardVO questionStandardVO = questionMapper.getStandardById(id);
        if (questionStandardVO == null) {
            String message = "要查看的问题不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return questionStandardVO;
    }

    @Override
    public List<QuestionListItemVO> list() {
        return questionMapper.list();
    }

}
